<!DOCTYPE html>
<html>
<?php $this->load->view('admin/public/header.html');?>

<body class="childrenBody">
<form class="layui-form">
	<blockquote class="layui-elem-quote quoteBox">
		<form class="layui-form">
			<div class="layui-inline">
				<div class="layui-input-inline">
					<input type="text" class="layui-input searchVal" placeholder="请输入要昵称或姓名" />
				</div>
				<a class="layui-btn search_btn" data-type="reload">搜索</a>
			</div>
			<!-- <div class="layui-inline">
				<a class="layui-btn layui-btn-normal addNews_btn">添加用户</a>
			</div>
			<div class="layui-inline">
				<a class="layui-btn layui-btn-danger layui-btn-normal delAll_btn">批量删除</a>
			</div> -->
		</form>
	</blockquote>
	
	
	
	<table id="newsList" lay-filter="newsList"></table>
	

	<!--操作-->
	<script type="text/html" id="newsListBar">
		<a class="layui-btn layui-btn-xs"  lay-event="check">查看详情</a>
		{{# if(d.userFlag==2){ }}
		<a class="layui-btn layui-btn-danger  layui-btn-xs hide able{{d.userFlag}}" lay-event="disable">驳回认证</a>
		{{# }else if(d.userFlag==4){ }}
		<a class="layui-btn  layui-btn-xs  hide disable{{d.userFlag}}"  lay-event="able">通过认证</a>
		{{# }else if(d.userFlag==1){ }}
			<a class="layui-btn  layui-btn-xs  hide disable{{d.userFlag}}"  lay-event="able">通过认证</a>
			<a class="layui-btn layui-btn-danger  layui-btn-xs hide able{{d.userFlag}}" lay-event="disable">驳回认证</a>

		{{# } }}



		<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">拉黑</a>
		 
	</script>
</form>
<script type="text/javascript">

layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table,
		page;
    //列表
    var tableIns = table.render({
        elem: '#newsList',
        url : '<?php echo site_url("admin/user/getCompanyUser")?>',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 20,
        limits : [10,15,20,25],
        
        id : "newsListTable",
        cols : [[
                 {field: 'userId', title: 'ID', width:100, align:"center",sort:true},
                 {field: 'nickName', title: '昵称'},
                 {field: 'realName', title: '真实姓名'}, 
                 {field: 'position', title: '所在城市'},
                 {field: 'phone', title: '手机号'},
                 {field: 'sex', title: '性别',templet:function(d){
                	 if(d.sex==1){
                		 return '男';
                	 }else if (d.sex==2) {
						return '女';
					}else{
						return '未知';
					}
                 }},     
                 {field: 'birthday', title: '生日'},
                 {field: 'id_number', title: '身份证号'},
                 {field: 'userFlag', title: '用户类型' ,templet:function(d){
                	 if(d.userFlag==1){
                		 return '企业用户(未认证)';
                	 }else if (d.userFlag==2) {
						return '企业用户(已认证)';
					}else if (d.userFlag==4) {
 						return '企业用户(认证未通过)';
 					}else{
						return '普通用户';
					}
                 } },
                 {title: '操作',width:300, field:'tools', templet:'#newsListBar',fixed:"right",align:"center"}
             ]],
             response: {
             	  statusName: 'code' //数据状态的字段名称，默认：code
             	  ,statusCode: 200 //成功的状态码，默认：0
             	  ,msgName: 'msg' //状态信息的字段名称，默认：msg
             	  ,countName: 'count' //数据总数的字段名称，默认：count
             	  ,dataName: 'data' //数据列表的字段名称，默认：data
             	} ,
         done:function(res,curr,count){
        	 page=curr;
         }
    });

    

    //搜索
    $(".search_btn").on("click",function(){
    	
    	table.reload("newsListTable",{
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                search: $(".searchVal").val()  //搜索的关键字
            }
        })
        
        
    });

    //添加用户
    function addNews(edit){
        var index = layui.layer.open({
            title : "用户信息",
            type : 2,
            content : "<?php echo site_url('admin/user/add')?>",		//iframe层时,这个参数传URL
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(edit){
                	if(edit.sex==1){
                		edit.sex='男';
                	}else if (edit.sex==2) {
						edit.sex='女';
					}else{
						edit.sex='未知';
					}
                	body.find(".userId").val(edit.userId);
                    body.find('[name=nickName]').val(edit.nickName);
                    body.find('[name=realName]').val(edit.realName);
                    body.find('[name=position]').val(edit.position);                    
                    body.find('[name=phone]').val(edit.phone);
                    body.find('[name=sex]').val(edit.sex);
                    body.find('[name=birthday]').val(edit.birthday);
                    body.find('[name=id_number]').val(edit.id_number);
                    body.find('[name=trade]').val(edit.typeName);
                    body.find('[name=job]').val(edit.job);
                    body.find('.companyImg').show();
                    if(edit.companyImg){
                    	body.find('.posterimg').attr('src','<?php echo IMAGEHOST?>'+edit.companyImg);
                    }
                    
                    form.render();
					
                }
                
                setTimeout(function(){
                    layui.layer.tips('点击返回', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }
    $(".addNews_btn").click(function(){
        addNews();
    })

    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('newsListTable'),
            data = checkStatus.data,
            userIds = [];
        //console.log(data);return;
        if(data.length > 0) {
            for (var i in data) {
            	userIds.push(data[i].userId);
            }
            layer.confirm('确定删除选中的用户？', {icon: 3, title: '提示信息'}, function (index) {
            	$.ajax({
                	type:'post',
                	data:{userIds:userIds},
                	dataType: "json", 
                	url:"<?php echo site_url('admin/user/del')?>",
                	success:function(res){
                		if(res.code==200){
                			tableIns.reload();
                            layer.close(index);
                		}else{
                			layer.msg('删除失败');
                		}
                		
                        
                	},
                	fail:function(){
                		layer.msg('删除失败');
                	}
                })
            })
        }else{
            layer.msg("请选择需要删除的用户");
        }
    })

    
   
    
    table.on('tool(newsList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;		//用户数据
        if(layEvent === 'check'){ //编辑
            addNews(data);
        } else if (layEvent === 'able') {
        	layer.confirm('确定通过用户认证吗',{icon:3, title:'提示信息'},function(index){
            	console.log(data.userId);
                $.ajax({
                	type:'post',
                	data:{userId:data.userId},
                	dataType: "json", 
                	url:"<?php echo site_url('admin/user/able')?>",
                	success:function(res){
                		if(res.code==200){
                			layer.close(index)
                			 table.reload("newsListTable",{
                				page: {
                				    curr: page //重新从第curr 页开始
                				}
                			}) 
                			 obj.update({
                			      userFlag:1,
                			      tools: '',
                			    });
                            layer.close(index);
                		}else{
                			layer.msg('操作失败');
                		}
                	},
                	fail:function(){
                		layer.msg('操作失败');
                	}
                })
                
            });
		} else if (layEvent === 'disable') {
			layer.confirm('确定驳回用户认证吗?',{icon:3, title:'提示信息'},function(index){
            	console.log(data.userId);
                $.ajax({
                	type:'post',
                	data:{userId:data.userId},
                	dataType: "json", 
                	url:"<?php echo site_url('admin/user/disable')?>",
                	success:function(res){
                		
                		if(res.code==200){
                			layer.close(index);
                			table.reload("newsListTable",{
                				page: {
                				    curr: page //重新从第curr 页开始
                				}
                			}) 
                			obj.update({
              			      userFlag:2,
              			      tools: '',
              			    });
                            
                		}else{
                			layer.msg('操作失败');
                		}
                	},
                	fail:function(){
                		layer.msg('操作失败');
                	}
                })
                
            });
		}else if(layEvent === 'del'){ //删除
            layer.confirm('确定拉黑此用户？',{icon:3, title:'提示信息'},function(index){
            	console.log(data.userId);
                $.ajax({
                	type:'post',
                	data:{userId:data.userId},
                	dataType: "json", 
                	url:"<?php echo site_url('admin/user/del')?>",
                	success:function(res){
                		if(res.code==200){
                			obj.del();
                            layer.close(index);
                		}else{
                			layer.msg('操作失败');
                		}
                	},
                	fail:function(){
                		layer.msg('操作失败');
                	}
                })
                
            });
        }
    });
   
    

})

</script>
</body>
</html>